package edu.gatech.doi.spamframework.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;

import org.jdom.input.SAXBuilder;

import weka.core.Instances;

import edu.gatech.doi.spamframework.filters.WekaDemo;
import edu.gatech.doi.spamframework.mapping.MySpaceMapper;
import edu.gatech.doi.spamframework.mapping.TrecMapper;
import edu.gatech.doi.spamframework.mapping.TwitterMapper;
import edu.gatech.doi.spamframework.mapping.WebMapper;
import edu.gatech.doi.spamframework.mapping.ZingMapper;
/**
 * the controller of spam detection framework for command lines.
 * transforming the datasets
 * @author DE WANG
 *
 */
public class SpamFrameworkController {

	
	private String datasourcetype;
	private String modeltype;
	private String datasetfilepath;
	public static String readLine()
	{
		String s = "";
		try {
			InputStreamReader converter = new InputStreamReader(System.in);
			BufferedReader in = new BufferedReader(converter);
			s = in.readLine();
		} catch (Exception e) {
			System.out.println("Error! Exception: "+e); 
		}
		return s;
	}


	
	public void execute()
	{
		 int datasourcetypeid=Integer.parseInt(datasourcetype);
		 int modeltypeid=Integer.parseInt(modeltype);
		 
//		 DataSources ds=new DataSources();
	
		 if(datasourcetypeid==DataSourceTypes.get("MYSPACE"))
		 {
			 System.out.println("Datasource is MySpace.");
			
			 
			 MySpaceMapper msm=new MySpaceMapper();
			 
			 msm.init();
			 if(modeltypeid==ModelTypes.get("PERSON"))
			 {
				 System.out.println("Seleted model is PERSON.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 if(substr1.equalsIgnoreCase("file")||substr1.equalsIgnoreCase("dir"))
				 {
					 msm.parsePersons(substr2);
				 }
				 else if(substr1.equalsIgnoreCase("indexfilename"))
				 {
					 msm.parsePersonsFromIndexFile(substr2);
				 }
				 
//				 File file=new File(this.datasetfilepath);
				 
			 }
			 
			 if(modeltypeid==ModelTypes.get("MESSAGE"))
			 {
				 System.out.println("Seleted model is MESSAGE.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 if(substr1.equalsIgnoreCase("file")||substr1.equalsIgnoreCase("dir"))
				 {
					 msm.parseMessages(substr2);
				 }
				 else if(substr1.equalsIgnoreCase("indexfilename"))
				 {
					 msm.parseMessagesFromIndexFile(substr2);
				 }
			 }
			 
		 }
		 if(datasourcetypeid==DataSourceTypes.get("TWITTER"))
		 {
			 System.out.println("Datasource is TWITTER.");
				
			 
			 TwitterMapper tm=new TwitterMapper();
				tm.init();
			 if(modeltypeid==ModelTypes.get("PERSON"))
			 {
				 System.out.println("Seleted model is PERSON.");
				 tm.parsePersons(this.datasetfilepath);
			 }
			 
			 if(modeltypeid==ModelTypes.get("MESSAGE"))
			 {
				 System.out.println("Seleted model is MESSAGE.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 System.out.println(str);
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 System.out.println(substr1+" "+substr2);
				 if(substr1.equalsIgnoreCase("file"))
				 {
					 tm.parseMessagesByStatusID(substr2);
				 }
				 else
				 {
					 tm.parseMessages(this.datasetfilepath);
				 }
				
			 }
			 
			 if(modeltypeid==ModelTypes.get("WEBPAGE"))
			 {
				 System.out.println("Seleted model is WEBPAGE.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 System.out.println(str);
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 System.out.println(substr1+" "+substr2);
				 if(substr1.equalsIgnoreCase("file"))
				 {
					 tm.parseWebPagesByUrlID(substr2);
				 }
				 else
				 {
					 tm.parseWebPages();
				 }
				 
				 
			 }
		 }
		 if(datasourcetypeid==DataSourceTypes.get("ZING"))
		 {
			 System.out.println("Datasource is ZING.");
				
			 ZingMapper zm=new ZingMapper();
			 zm.init();
			 if(modeltypeid==ModelTypes.get("PERSON"))
			 {
				 System.out.println("Seleted model is PERSON.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 if(substr1.equalsIgnoreCase("file")||substr1.equalsIgnoreCase("dir"))
				 {
					 zm.parsePersons(substr2);
				 }
				 else if(substr1.equalsIgnoreCase("indexfilename"))
				 {
					 zm.parsePersonsFromIndexFile(substr2);
				 }
				 
				 zm.parsePersons(this.datasetfilepath);
			 }
			 
			 if(modeltypeid==ModelTypes.get("MESSAGE"))
			 {
				 System.out.println("Seleted model is MESSAGE.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 if(substr1.equalsIgnoreCase("file")||substr1.equalsIgnoreCase("dir"))
				 {
					 zm.parseMessages(substr2);
				 }
				 else if(substr1.equalsIgnoreCase("indexfilename"))
				 {
					 zm.parseMessagesFromIndexFile(substr2);
				 }
				
			 }
		 }
			
		 if(datasourcetypeid==DataSourceTypes.get("TREC"))
		 {
			 System.out.println("Datasource is TREC.");
			 TrecMapper tm=new TrecMapper();
			 tm.init();
			 if(modeltypeid==ModelTypes.get("PERSON"))
			 {
				 System.out.println("Seleted model is PERSON.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 if(substr1.equalsIgnoreCase("file"))
				 {
					 tm.parsePersons(substr2);
				 }
				 else if(substr1.equalsIgnoreCase("dir"))
				 {
					 File file=new File(substr2);
					 File[] filelist=file.listFiles();
			    
			    	for(int j=0;j<filelist.length;j++)
			    	{
			    		
			    			String tempfilename=filelist[j].getPath();
			    			tm.parsePersons(tempfilename);
			    		
			    	}
				 }
				 else if(substr1.equalsIgnoreCase("indexfilename"))
				 {
					 tm.parsePersonsFromIndexFile(substr2);
				 }
			 }
			 
			 if(modeltypeid==ModelTypes.get("MESSAGE"))
			 {
				 System.out.println("Seleted model is MESSAGE.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 System.out.println(str);
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 System.out.println(substr1+" "+substr2);
				 if(substr1.equalsIgnoreCase("file"))
				 {
					 tm.parseMessages(substr2);
				 }
				 else if(substr1.equalsIgnoreCase("dir"))
				 {
					 File file=new File(substr2);
					 File[] filelist=file.listFiles();
			    
			    	for(int j=0;j<filelist.length;j++)
			    	{
			    		
			    			String tempfilename=filelist[j].getPath();
			    			tm.parsePersons(tempfilename);
			    		
			    	}
				 }
				 else if(substr1.equalsIgnoreCase("indexfilename"))
				 {
					 tm.parseMessagesFromIndexFile(substr2);
				 }
			 }
		 }

		 if(datasourcetypeid==DataSourceTypes.get("WEBPAGE"))
		 {
			 System.out.println("Datasource is WEBPAGE.");
			 WebMapper wm=new WebMapper();
			 wm.init();

			 if(modeltypeid==ModelTypes.get("WEBPAGE"))
			 {
				 System.out.println("Seleted model is WEBPAGE.");
				 System.out.println("Please input the file path for dataset, three types of input: filename, directory, indexfilename[e.g file=... or dir=... or indexfilename=... ]: ");
				 String str=readLine();
				 System.out.println(str);
				 int eqIndex = str.indexOf('=');
				 String substr1=str.substring(0, eqIndex).trim();
				 String substr2=str.substring(eqIndex+1).trim();
				 System.out.println(substr1+" "+substr2);
				 if(substr1.equalsIgnoreCase("file"))
				 {
					 wm.parseWebPages(substr2);
				 }
				 else if(substr1.equalsIgnoreCase("dir"))
				 {
					 File file=new File(substr2);
					 File[] filelist=file.listFiles();
			    
			    	for(int j=0;j<filelist.length;j++)
			    	{
			    		
			    			String tempfilename=filelist[j].getPath();
			    			wm.parseWebPages(tempfilename);
			    		
			    	}
				 }
				 else if(substr1.equalsIgnoreCase("indexfilename"))
				 {
					 wm.parseWebPagesFromIndexFile(substr2);
				 }
				 
			 }
			 
		 }
			 
		 
		 
		 
	}
	
	
	public static String usage()
	{
		return
        "\nUsage:\n  " + SpamFrameworkController.class.getName() 
        + " \n DATASOURCETYPE <typeid:int>  \n"
        + "  MODELTYPE <typeid:int> \n";
     
	}
	
	
	
	
	
	
	public String getDatasourcetype() {
		return datasourcetype;
	}






	public void setDatasourcetype(String datasourcetype) {
		this.datasourcetype = datasourcetype;
	}






	public String getModeltype() {
		return modeltype;
	}






	public void setModeltype(String modeltype) {
		this.modeltype = modeltype;
	}






	public String getDatasetfilepath() {
		return datasetfilepath;
	}






	public void setDatasetfilepath(String datasetfilepath) {
		this.datasetfilepath = datasetfilepath;
	}



	


	/**
	 * 1: datasource type;2: model type 2 dataset file path.
	 * @param arg0
	 */
	
	
	public static void main(String [] args)
	{
		 if (args.length < 4) {
		      System.out.println(SpamFrameworkController.usage());
		      System.exit(1);
		    }
		// parse command line
		    String datasourcetype = "";
		    String modeltype = "";
		    String datasetfilepath = "";
		   

		    int i = 0;
		    String current = "";
		    boolean newPart = false;
		    do {
		      // determine part of command line
		      if (args[i].equals("DATASOURCETYPE")) {
		        current = args[i];
		        i++;
		        newPart = true;
		      }
		      else if (args[i].equals("MODELTYPE")) {
		        current = args[i];
		        i++;
		        newPart = true;
		      }



		      if (current.equals("DATASOURCETYPE")) {
		    	   if (newPart)
		          datasourcetype = args[i];
		        
		      }
		      else if (current.equals("MODELTYPE")) {
		    	   if (newPart)
		          modeltype = args[i];
		       
		      }



		      // next parameter
		      i++;
		      newPart = false;
		    } 
		    while (i < args.length);
		
		 // everything provided?
//		    System.out.println(datasourcetype+" "+modeltype);
		 if ( datasourcetype.equals("") || modeltype.equals("")  ) {
		      System.out.println("Not all parameters provided!");
		      System.out.println(SpamFrameworkController.usage());
		      System.exit(2);
		    }
		 
		 // run
		 SpamFrameworkController sfc=new SpamFrameworkController();
		 
		 sfc.setDatasourcetype(datasourcetype);
		 sfc.setModeltype(modeltype);
//		 sfc.setDatasetfilepath(datasetfilepath);
		 long start, end, total;
		 start = System.currentTimeMillis();
		 sfc.execute();
		 end = System.currentTimeMillis();
		 total = end - start;
		 System.out.println("Consumed Time(ms):"+total);

		 
		 
		 
	}
}
